// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "CIrrDevicePS3.h"

#ifdef _IRR_COMPILE_WITH_PS3_DEVICE_

#include <stdio.h>
#include <stdlib.h>
#include "IEventReceiver.h"
#include "os.h"
#include "CTimer.h"
#include "irrString.h"
#include "Keycodes.h"
#include "COSOperator.h"
#include "CColorConverter.h"
#include "SIrrCreationParameters.h"

namespace irr
{
	namespace video
	{
		#ifdef _IRR_COMPILE_WITH_OGLES1_
		IVideoDriver* createOGLES1Driver(const SIrrlichtCreationParameters& params,
			video::SExposedVideoData& data, io::IFileSystem* io);
		#endif
		#ifdef _IRR_COMPILE_WITH_OGLES2_
		IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params,
			video::SExposedVideoData& data, io::IFileSystem* io);
		#endif
	}
}


namespace irr
{

//! constructor
CIrrDevicePS3::CIrrDevicePS3(const SIrrlichtCreationParameters& param): CIrrDeviceStub(param),
	Width(param.WindowSize.Width), Height(param.WindowSize.Height),
	WindowHasFocus(false), WindowMinimized(false),
	UseXVidMode(false), UseXRandR(false), UseGLXWindow(false),
	ExternalWindow(false), AutorepeatSupport(0)
{
	#ifdef _DEBUG
	setDebugName("CIrrDevicePS3");
	#endif

	// create keymap
	createKeyMap();

	// create window
	if (CreationParams.DriverType != video::EDT_NULL)
	{
		// create the window, only if we do not use the null device
		if (!createWindow())
			return;
	}

	// create cursor control
	CursorControl = new CCursorControl(this, CreationParams.DriverType == video::EDT_NULL);

	// create driver
	createDriver();

	if (!VideoDriver)
		return;

	createGUIAndScene();
}


//! destructor
CIrrDevicePS3::~CIrrDevicePS3()
{
	//#TODO
}

bool CIrrDevicePS3::switchToFullscreen(bool reset)
{
	if (!CreationParams.Fullscreen)
		return true;

	//#TODO

	return CreationParams.Fullscreen;
}

bool CIrrDevicePS3::createWindow()
{
	//#TODO

	return true;
}


//! create the driver
void CIrrDevicePS3::createDriver()
{
	switch(CreationParams.DriverType)
	{
	case video::EDT_OPENGL:
		os::Printer::log("This driver is not available in PS3.",
			ELL_ERROR);
		break;

	case video::EDT_DIRECT3D9:
		os::Printer::log("This driver is not available in PS3.",
			ELL_ERROR);
		break;

	case video::EDT_OGLES1:
		{
		#ifdef _IRR_COMPILE_WITH_OGLES1_
		video::SExposedVideoData data;
		VideoDriver = video::createOGLES1Driver(CreationParams, data, FileSystem);
		#else
		os::Printer::log("No OpenGL ES 1 support compiled in.", ELL_ERROR);
		#endif
		}
		break;

	case video::EDT_OGLES2:
		{
		#ifdef _IRR_COMPILE_WITH_OGLES2_
		video::SExposedVideoData data;
		VideoDriver = video::createOGLES2Driver(CreationParams, data, FileSystem);
		#else
		os::Printer::log("No OpenGL ES 2 support compiled in.", ELL_ERROR);
		#endif
		}
		break;

	case video::EDT_NULL:
		VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
		break;

	default:
		os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
		break;
	}
}

//! runs the device. Returns false if device wants to be deleted
bool CIrrDevicePS3::run()
{
	os::Timer::tick();

	//#TODO

	return !Close;
}


//! Pause the current process for the minimum time allowed only to allow other processes to execute
void CIrrDevicePS3::yield()
{
	//#TODO
}


//! Pause execution and let other processes to run for a specified amount of time.
void CIrrDevicePS3::sleep(u32 timeMs, bool pauseTimer=false)
{
	const bool wasStopped = Timer ? Timer->isStopped() : true;

	if (pauseTimer && !wasStopped)
		Timer->stop();

	//#TODO

	if (pauseTimer && !wasStopped)
		Timer->start();
}


//! sets the caption of the window
void CIrrDevicePS3::setWindowCaption(const wchar_t* text)
{
	//#TODO
}

//! notifies the device that it should close itself
void CIrrDevicePS3::closeDevice()
{
	Close = true;
}


//! returns if window is active. if not, nothing need to be drawn
bool CIrrDevicePS3::isWindowActive() const
{
	return (WindowHasFocus && !WindowMinimized);
}


//! returns if window has focus.
bool CIrrDevicePS3::isWindowFocused() const
{
	return WindowHasFocus;
}


//! returns if window is minimized.
bool CIrrDevicePS3::isWindowMinimized() const
{
	return WindowMinimized;
}


//! returns color format of the window.
video::ECOLOR_FORMAT CIrrDevicePS3::getColorFormat() const
{
	//#TODO
	return video::ECF_A8R8G8B8;
}


//! Sets if the window should be resizable in windowed mode.
void CIrrDevicePS3::setResizable(bool resize)
{
	//#TODO
}


//! Return pointer to a list with all video modes supported by the gfx adapter.
video::IVideoModeList* CIrrDevicePS3::getVideoModeList()
{
	//#TODO
	return &VideoModeList;
}


//! Minimize window
void CIrrDevicePS3::minimizeWindow()
{
	//#TODO
}


//! Maximize window
void CIrrDevicePS3::maximizeWindow()
{
	//#TODO
}


//! Restore original window size
void CIrrDevicePS3::restoreWindow()
{
	//#TODO
}


void CIrrDevicePS3::createKeyMap()
{
	//#TODO
}

bool CIrrDevicePS3::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
{
	//#TODO
	return false;
}

//! Set the current Gamma Value for the Display
bool CIrrDevicePS3::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )
{
	//#TODO
	return false;
}


//! Get the current Gamma Value for the Display
bool CIrrDevicePS3::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )
{
	brightness = 0.f;
	contrast = 0.f;
	red = 0.0f;
	green = 0.0f;
	blue = 0.0f;

	//#TODO

	return false;
}

} // end namespace

#endif // _IRR_COMPILE_WITH_X11_DEVICE_

